package com.example.shopping_web_back.controller;

import com.example.shopping_web_back.pojo.*;
import com.example.shopping_web_back.service.GoodsService;
import com.example.shopping_web_back.service.OrderService;
import com.example.shopping_web_back.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order")
@Validated
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private GoodsService goodsService;

    @GetMapping("/gotDataByPage")
    public Result<PageBean<OrdersOfGoods>> list(
            Integer pageNum, //当前页
            Integer pageSize //每页个数
    ) {
        PageBean<OrdersOfGoods> pbOrders = orderService.list(pageNum, pageSize);
        return Result.success(pbOrders);
    }

    @GetMapping("/gotDataByPageBusiness")
    public Result<PageBean<OrdersOfGoods>> listBusiness(
            Integer pageNum, //当前页
            Integer pageSize //每页个数
    ) {
        PageBean<OrdersOfGoods> pbOrders = orderService.listBusiness(pageNum, pageSize);
        return Result.success(pbOrders);
    }

    @GetMapping("/gotTotalSalesPrice")
    public Result<Double> gotTotalSalesPrice() {
        Map<String, Object> map = ThreadLocalUtil.get(); //先得到用户
        String businessName = (String) map.get("Business_name");
        double totalSalesPrice = orderService.gotTotalSalesPrice(businessName);
        return Result.success(totalSalesPrice);
    }

    @GetMapping("/gotSixMonthSalesPrice")
    public Result<List<MonthlySalesPriceDTO>> gotSixMonthSalesPrice() {
        Map<String, Object> map = ThreadLocalUtil.get();
        String businessName = (String) map.get("Business_name");
        List<MonthlySalesPriceDTO> SixMonthSalesPrice = orderService.gotSixMonthSalesPrice(businessName);
        return Result.success(SixMonthSalesPrice);
    }

    @GetMapping("/gotTotalSalesNum")
    public Result<Integer> gotTotalSalesNum() {
        Map<String, Object> map = ThreadLocalUtil.get();
        String businessName = (String) map.get("Business_name");
        Integer totalSalesNum = orderService.gotTotalSalesNum(businessName);
        return Result.success(totalSalesNum);
    }

    @GetMapping("/gotSixMonthSalesNum")
    public Result<List<MonthlySalesNumDTO>> gotSixMonthSalesNum() {
        Map<String, Object> map = ThreadLocalUtil.get();
        String businessName = (String) map.get("Business_name");
        List<MonthlySalesNumDTO> SixMonthSalesNum = orderService.gotSixMonthSalesNum(businessName);
        return Result.success(SixMonthSalesNum);
    }

    @PostMapping("/addOrder")
    public Result addOrder(@RequestBody @Validated OrdersOfGoods ordersOfGoods){
        Map<String, Object> map = ThreadLocalUtil.get(); //先得到用户名
        String customerName = (String)map.get("customer_name");
        ordersOfGoods.setCustomerName(customerName);
        //更新销量
        Goods goods = goodsService.getGoodsDataByBusiness(ordersOfGoods.getBusinessName(), ordersOfGoods.getGoodsName());
        goods.setSalesNum(goods.getSalesNum() + ordersOfGoods.getGoodsNum());
        goodsService.updateGoods(ordersOfGoods.getGoodsName(), goods);
        orderService.addOrder(ordersOfGoods);
        return Result.success();
    }

    @PostMapping("/updateOrder")
    public Result updateOrder(
            @RequestParam(required = false) String arriveTime,
            @RequestParam(required = false) String emailTime,
            @RequestParam(required = false) String shippingTime,
            @RequestParam(required = false) String deliveryTime,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String customerName,
            @RequestParam(required = true) String buyTime,
            @RequestParam(required = true) String businessName,
            @RequestParam(required = true) String goodsName
    ){
        if(customerName == null) {
            Map<String, Object> map = ThreadLocalUtil.get(); //先得到用户
            String new_customerName = (String) map.get("customer_name");
            orderService.updateOrder(arriveTime, emailTime, shippingTime, deliveryTime, status, buyTime, businessName, goodsName, new_customerName);
            return Result.success();
        }
        orderService.updateOrder(arriveTime, emailTime, shippingTime, deliveryTime, status, buyTime, businessName, goodsName, customerName);
        return Result.success();
    }

    @PostMapping("/deleteOrder")
    public Result deleteOrder(@RequestBody @Validated OrdersOfGoods ordersOfGoods){
        orderService.deleteOrder(ordersOfGoods);
        return Result.success();
    }
}
